package com.yxq.mdt.app.base;


import android.graphics.Typeface;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.google.android.material.tabs.TabLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.ibase.IBaseContract;
import com.yxq.mdt.app.widgets.ViewPagerFixed;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 通用的带tab的Viewpager滑动界面
 *
 * @param <T> 需要的TabBean
 * @param <F> 需要的Fragment
 */
public abstract class BaseTabActivity<T extends BaseTabBean, F extends Fragment, P extends IBaseContract.IBasePresenter> extends BaseActivity<P> {


    @BindView(R.id.arch_home_tab_layout)
    protected TabLayout mTabLayout;


    @BindView(R.id.arch_vp_view_pager)
    protected ViewPagerFixed mViewPager;


    protected List<T> mTabBeanList;
    protected BaseViewPagerAdapter mPagerAdapter;


    public class BaseViewPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> mFragments;

        public BaseViewPagerAdapter(FragmentManager fm) {
            super(fm);
            mFragments = new ArrayList<>();
        }

        public void addPage(Fragment fragment) {
            mFragments.add(fragment);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public Fragment getItem(int i) {
            return mFragments.get(i);
        }


        @Override
        public CharSequence getPageTitle(int position) {
            return mTabBeanList.get(position).tabName;
        }

    }


    @Override
    public int getLayoutId() {
        setStatusBarDarkFont(true);
        return R.layout.activity_common_tab_layout;
    }

    @Override
    public void initView() {

        initParams();
        if (!isSyncInitTabBean()) {
            initLayout();
        }
    }

    /**
     * 是否需要异步初始化tab，如果需要，那就需要自己手动调用initLayout
     *
     * @return
     */
    protected boolean isSyncInitTabBean() {
        return false;
    }

    //需要子类实现的Tab数据
    public abstract List<T> getTabList();

    //需要子类实现的列表fragment
    public abstract F getFragment(T tab);

    public final void initLayout() {

        //获取Tab数据
        mTabBeanList = getTabList();
        mPagerAdapter = new BaseViewPagerAdapter(getSupportFragmentManager());

        if (mTabBeanList != null) {
            //循环初始化tab和fragment
            for (T temp : mTabBeanList) {
                //mTabSegment.addTab(new QMUITabSegment.Tab(temp.tabName));
                mPagerAdapter.addPage(getFragment(temp));
            }
        }

        mViewPager.setAdapter(mPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

        //设置首选项字体颜色大小
        TextView title = (TextView) (((LinearLayout) ((LinearLayout) mTabLayout.getChildAt(0)).getChildAt(0)).getChildAt(1));
//        title.setTextSize(20);
        title.setTextColor(getResources().getColor(R.color.color_font_black));
        title.setTextAppearance(mActivity, Typeface.BOLD);
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //设置选中字体颜色大小
                TextView title = (TextView) (((LinearLayout) ((LinearLayout) mTabLayout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
//                title.setTextSize(20);
                title.setTextColor(getResources().getColor(R.color.color_font_black));
                title.setTextAppearance(mActivity, Typeface.BOLD);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //设置未选中字体颜色大小
                TextView title = (TextView) (((LinearLayout) ((LinearLayout) mTabLayout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
//                title.setTextSize(20);
                title.setTextColor(getResources().getColor(R.color.color_font_black_666));
                title.setTextAppearance(mActivity, Typeface.NORMAL);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    /**
     * 初始化参数
     */
    public void initParams() {
//        mTabLayout.setTabTextColors(R.color.color_font_black_666, R.color.color_font_black);
    }

}
